package codegen.ui.nbvisual.visual;

import java.awt.*;
import java.util.List;
import org.netbeans.api.visual.anchor.Anchor;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.model.ObjectState;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import codegen.graphnetwork.model.*;
/**
 *
 * @author cuong.nguyenmanh2
 */
public class CodeGenPinWidget extends Widget {

    private LabelWidget nameWidget;
    private CodeGenGlyphSetWidget glyphsWidget;
    private CodeGenAnchor anchor;
    private final CircleWidget selectPinWidget;
    private LayerWidget childLayer;
    private LayerWidget selectLayer;
    private Anchor selectAnchor;
    private Value value;

    /**
     * Creates a pin widget with a specific color scheme.
     *
     * @param scene the scene
     * @param scheme the color scheme
     */
    public CodeGenPinWidget(CodeGenGraphScene scene, Value value) {
        super(scene);

        this.value = value;
        setLayout(LayoutFactory.createAbsoluteLayout());
        nameWidget = new LabelWidget(scene);

        int selectSize = 4;
        selectPinWidget = new CircleWidget(scene, selectSize);
        childLayer = new LayerWidget(scene);
        selectLayer = new LayerWidget(scene);
        childLayer.addChild(nameWidget);
        childLayer.setLayout(LayoutFactory.createOverlayLayout());

        selectLayer.setLayout(LayoutFactory.createAbsoluteLayout());
        selectLayer.addChild(selectPinWidget);
        //selectPinWidget.setPreferredBounds(new Rectangle(128 - selectSize, 12 - selectSize, 128 + selectSize, 12 + selectSize));
        Dimension size = new Dimension(128, 25);
        if (!value.isInput) {
            selectPinWidget.setBackground(Color.YELLOW);
            selectPinWidget.setPreferredLocation(new Point(size.width, size.height / 2));
        } else {
            selectPinWidget.setBackground(Color.BLUE);
            selectPinWidget.setPreferredLocation(new Point(-8, size.height / 2));
        }
        //selectPinWidget.setToolTipText("Drag to create new Link !");
        selectAnchor = AnchorFactory.createCenterAnchor(selectPinWidget);
        //addChild(glyphsWidget = new CodeGenGlyphSetWidget(scene));
        addChild(childLayer);
        addChild(selectLayer);

        setMinimumSize(size);
        nameWidget.setForeground(Color.yellow);
        //Color cyan80 = new Color(0.2f, 0.2f, 1f, 0.5f);
        //setBackground(cyan80);
        notifyStateChanged(ObjectState.createNormal(), ObjectState.createNormal());
        //setOpaque(true);

    }

    /**
     * Called to notify about the change of the widget state.
     *
     * @param previousState the previous state
     * @param state the new state
     */
    protected void notifyStateChanged(ObjectState previousState, ObjectState state) {
    }

    /**
     * Returns a pin name widget.
     *
     * @return the pin name widget
     */
    public Widget getPinNameWidget() {
        return nameWidget;
    }

    /**
     * Sets a pin name.
     *
     * @param name the pin name
     */
    public void setPinName(String name) {
        nameWidget.setLabel(name);
    }

    /**
     * Returns a pin name.
     *
     * @return the pin name
     */
    public String getPinName() {
        return nameWidget.getLabel();
    }

    /**
     * Sets pin glyphs.
     *
     * @param glyphs the list of images
     */
    public void setGlyphs(List<Image> glyphs) {
        glyphsWidget.setGlyphs(glyphs);
    }

    /**
     * Sets all pin properties at once.
     *
     * @param name the pin name
     * @param glyphs the pin glyphs
     */
    public void setProperties(String name, List<Image> glyphs) {
        setPinName(name);
        glyphsWidget.setGlyphs(glyphs);
    }

    /**
     * Creates a horizontally oriented anchor similar to
     * VMDNodeWidget.createAnchorPin
     *
     * @return the anchor
     */
    public Anchor createAnchor() {
        if (anchor == null) {
            anchor = new CodeGenAnchor(this);
        }
        return anchor;
    }

    public CircleWidget getSelectPinWidget() {
        return selectPinWidget;
    }

    public Anchor getPinAnchor() {
        return selectAnchor;
    }
}
